
package com.infitecs.eyas.common.listener;


import com.infitecs.eyas.constant.Constant;
import com.infitecs.eyas.expression.dao.ExpressionDao;
import com.infitecs.eyas.expression.model.Formula;
import com.infitecs.eyas.runcoalbin.dao.CoalBunkerDao;
import com.infitecs.eyas.runcoalbin.dao.CoalBunkerResultDao;
import com.infitecs.eyas.runcoalbin.model.CoalBunker;
import com.infitecs.eyas.runcoalbin.model.CoalBunkerResult;
import com.infitecs.eyas.security.model.User;
import fr.expression4j.core.Expression;
import fr.expression4j.core.exception.ParsingException;
import fr.expression4j.factory.ExpressionFactory;
import org.apache.tools.ant.taskdefs.condition.Http;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 *
 *
 * @author longshihui
 * @version 1.0.1
 */
public class SessionListener implements HttpSessionListener {

    public WebApplicationContext getStringContext(HttpSession session) {
        ServletContext servletContext = session.getServletContext();
        return WebApplicationContextUtils.getWebApplicationContext(servletContext);
    }

    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        HttpSession session = httpSessionEvent.getSession();
//        initBoilerFormula(session);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        HttpSession session = httpSessionEvent.getSession();
        User user = (User) session.getAttribute("user");
        if (user != null) {
            ServletContext context = session.getServletContext();
            Map logonUsers = (HashMap) context.getAttribute("logonUsers");
            if (logonUsers == null) {
                logonUsers = new HashMap();
            }
            if (session.getId().equals(logonUsers.get(user.getUsername()))) {
                logonUsers.remove(user.getUsername());
                context.setAttribute("logonUsers", logonUsers);
            }
        }
        Map<String,Class> VONameDaoClass = new HashMap<>();
        VONameDaoClass.put(Constant.COALBUNKER_VO_NAME,CoalBunkerDao.class);
        VONameDaoClass.put(Constant.COALBUNKERRESULT_VO_NAME,CoalBunkerResult.class);
        saveSessionVO(session, VONameDaoClass);
    }

    private void initBoilerFormula(HttpSession session){
        Formula formula = new Formula();
        formula.setFormulaName(Constant.FORMULA_BOILER);
        WebApplicationContext springContext = getStringContext(session);
        ExpressionDao expressionDao = springContext.getBean(ExpressionDao.class);
        List<Formula> formulas = expressionDao.getFormulaListPage(formula);
        Map<String, Expression> boilerExpressions = new HashMap<>();
        for (Formula f : formulas) {
            boilerExpressions.put(f.getFormulaName(),analysisFormula(f));
        }
        session.setAttribute(Constant.FORMULA_BOILER,boilerExpressions);
    }

    private Expression analysisFormula(Formula formula){
        Expression expression = null;
        try {
            expression = ExpressionFactory.createExpression(formula.getParamer() + formula.getFormulaDescription());
        } catch (ParsingException e) {
            System.out.println(formula.getFormulaName() + " 123456");
        }
        return expression;
    }

    private void saveSessionVO(HttpSession session,Map<String,Class> VONameDaoClass){
        WebApplicationContext springContext = getStringContext(session);

        CoalBunker coalBunker = (CoalBunker) session.getAttribute("coalBunker");
        CoalBunkerResult coalBunkerResult = (CoalBunkerResult) session.getAttribute("coalBunkerResult");

        CoalBunkerDao coalBunkerDao = springContext.getBean(CoalBunkerDao.class);
        CoalBunkerResultDao coalBunkerResultDao = springContext.getBean(CoalBunkerResultDao.class);

        if (coalBunker != null) {
            coalBunkerDao.updateCoalBunker(coalBunker);
        }
        if (coalBunkerResult != null) {
            coalBunkerResultDao.updateCoalBunkerResult(coalBunkerResult);
        }
    }
}
